package org.qhhc.functioninterface;

import java.util.function.Function;

public class functionTest {
    public static void main(String[] args) {
        // 示例1: 字符串转大写（输入输出均为String类型）
        Function<String, String> toUpperCase = String::toUpperCase;
        System.out.println("转大写结果: " + toUpperCase.apply("hello function")); // HELLO FUNCTION[1](@ref)

        // 示例2: 字符串转整数（String → Integer）
        Function<String, Integer> parseInteger = Integer::parseInt;
        int number = parseInteger.apply("2024");
        System.out.println("转换数值: " + number); // 2024[2,5](@ref)

        // 示例3: 组合函数（先转大写→再取长度）
        Function<String, Integer> compositeFunc = toUpperCase.andThen(String::length);
        System.out.println("组合操作结果: " + compositeFunc.apply("Java 8")); // 5[8](@ref)

        // 示例4: 多层组合（字符串转整数→平方→转二进制）
        Function<Integer, String> squareAndBinary = num -> {
            int squared = num * num;
            return Integer.toBinaryString(squared);
        };
        Function<String, String> fullPipeline = parseInteger.andThen(squareAndBinary);
        System.out.println("2024平方的二进制: " + fullPipeline.apply("2024")); // 100111100100000000[5,8](@ref)

        // 示例5: 自定义对象转换
        Function<Employee, String> employeeInfo = emp ->
                emp.getName() + "(" + emp.getDepartment() + ")";
        System.out.println(employeeInfo.apply(new Employee("张三", "技术部"))); // 张三(技术部)[7](@ref)
    }

    static class Employee {
        private String name;
        private String department;

        public Employee(String name, String department) {
            this.name = name;
            this.department = department;
        }

        public String getName() { return name; }
        public String getDepartment() { return department; }
    }
}